package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBillDeliveryAudit;
import cn.stylefeng.guns.sys.modular.system.mapper.DsstoreBillDeliveryAuditMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillDeliveryAuditParam;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillDeliveryAuditResult;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillDeliveryResult;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryAuditService;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-07-14
 */
@Service
public class DsstoreBillDeliveryAuditServiceImpl extends ServiceImpl<DsstoreBillDeliveryAuditMapper, DsstoreBillDeliveryAudit> implements DsstoreBillDeliveryAuditService {


    @Autowired
    private DsstoreBillDeliveryService dsstoreBillDeliveryService;

    @Override
    public void add(DsstoreBillDeliveryAuditParam param) {
        DsstoreBillDeliveryAudit entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DsstoreBillDeliveryAuditParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DsstoreBillDeliveryAuditParam param) {
        DsstoreBillDeliveryAudit oldEntity = getOldEntity(param);
        DsstoreBillDeliveryAudit newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DsstoreBillDeliveryAuditResult findBySpec(DsstoreBillDeliveryAuditParam param) {
        return null;
    }

    @Override
    public List<DsstoreBillDeliveryAuditResult> findListBySpec(DsstoreBillDeliveryAuditParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DsstoreBillDeliveryAuditParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData audit(DsstoreBillDeliveryAuditParam param) {
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        DsstoreBillDeliveryAudit entity = getEntity(param);

        //TODO-1 不许二次审核，如果已经有该数据就不能保存成功
        DsstoreBillDeliveryAuditResult resultList = this.baseMapper.findListByParam(param);
        //DsstoreBillDeliveryAuditResult resultList = this.baseMapper.findListByVid(param);
        if (resultList == null) {
            this.saveOrUpdate(entity);
        } else {
            return ResponseData.error("您选择的数据已审核过，无需重复审核");
        }
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData batchAudit(DsstoreBillDeliveryAuditParam param) {
        //审核成功的门店
        //String failedSendName = "";
        int successNum = 0;
        //审核失败的门店数
        int notSuccessNum = 0;

        //TODO-1 修改状态为已发送
        String[] strArray = param.getIds().split(",");
        List<String> ids = Arrays.asList(strArray);

        List<DsstoreBillDeliveryResult> resultList = dsstoreBillDeliveryService.getViewDataListById(ids);
        Map<String, DsstoreBillDeliveryResult> listMap = new HashMap<>();
        for (DsstoreBillDeliveryResult result : resultList) {
            listMap.put(result.getId(), result);
        }

        for (String id : ids) {
            //DsstoreBillDeliveryResult result = dsstoreBillDeliveryService.getViewDataById(id);
            DsstoreBillDeliveryResult result = listMap.get(id);

            //已经通过的不能再次发送
            if (result.getStatus() != null && result.getStatus() == 1) {
                notSuccessNum = notSuccessNum + 1;
                continue;
            }
            //改状态为 1：已发送
            result.setStatus(1);
            this.updateViewDataById(result);
            successNum = successNum + 1;
        }

        if (notSuccessNum > 0 && successNum != 0) {
            return new SuccessResponseData(201, "审核成功数：" + successNum + "条,</br>审核失败数：" + notSuccessNum + "条,</br>失败原因：已审核过的数据，无需重复审核", null);
        } else if (notSuccessNum > 0 && successNum == 0) {
            return new SuccessResponseData(201, "您选择的数据已审核过，无需重复审核", null);
        }

        return ResponseData.success();
    }

    private void updateViewDataById(DsstoreBillDeliveryResult result) {
        DsstoreBillDeliveryAudit entity = new DsstoreBillDeliveryAudit();
        ToolUtil.copyProperties(result, entity);
        entity.setCreateUser(LoginContextHolder.getContext().getUserId());
        this.save(entity);
    }

    private Serializable getKey(DsstoreBillDeliveryAuditParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DsstoreBillDeliveryAudit getOldEntity(DsstoreBillDeliveryAuditParam param) {
        return this.getById(getKey(param));
    }

    private DsstoreBillDeliveryAudit getEntity(DsstoreBillDeliveryAuditParam param) {
        DsstoreBillDeliveryAudit entity = new DsstoreBillDeliveryAudit();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
